package com.ybc.chinese.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ybc.chinese.R;
import com.ybc.chinese.utils.Contants;
import com.ybc.chinese.utils.ImageUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * <p><p/>
 * Created by Yubch on 17/2/20.
 */
public class GalleryImageFragment extends BaseFragment {

    @Bind(R.id.iv_pic)
    public ImageView image;

    private String url;

    public static GalleryImageFragment newInstance(String url) {
        GalleryImageFragment fragment = new GalleryImageFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Contants.INTENT_URL, url);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getArguments().getString(Contants.INTENT_URL);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gallery_image, container, false);
        ButterKnife.bind(this, view);
        if(!TextUtils.isEmpty(url)) {
            if(url.contains(".gif")) {
                ImageUtil.displayGif(view.getContext(), image, url, R.mipmap.ic_default);
            } else {
                ImageUtil.displayPic(view.getContext(), image, url, R.mipmap.ic_default);
            }
        }
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        ButterKnife.unbind(this);
        super.onDestroyView();
    }
}
